//---------------------------------------------------------------------------

#pragma hdrstop
#include "Parameter.h"
#include "Controller.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)

UINT8 DeviceAddr[3];
Tparameter* Tparameter::m_ParamObj  = NULL;
Tparameter* Tparameter::GetInstance()
{
    if(m_ParamObj == NULL)
    {
       m_ParamObj = new  Tparameter();
    
    }

    return m_ParamObj;
}

Tparameter::Tparameter():CBusiness()
{

     m_hdr->m_FrameHeader[0] = 0xfe;
     m_hdr->m_FrameHeader[1] = 0x7f;
     m_hdr->m_FrameHeader_Len = 2;
     m_hdr->m_FrameLength = 11;
     m_hdr->m_type = 2;
     EnableSaveParamFlag = false;
     fp = NULL;
    // AnsiString fileStr =    ExtractFileDir(Application->ExeName)+"\\param.ini";
   // fp = new TIniFile(fileStr);

}
void Tparameter::CreateIniFile(AnsiString fileName)
{
     fp = NULL;
     fp = new TIniFile(fileName);
}
Tparameter::~Tparameter()
{
   delete fp;
   fp = NULL;
}
UINT8 Tparameter::Check_Sum(UINT8* buf,int len)
{
    UINT8 Sum = 0;
    for(int i=0;i<len;i++)
    {
        Sum += buf[i];
    }
    return Sum;
}
bool Tparameter::isValid(UINT8* buf,int len)
{
   if(buf[len-1] == Check_Sum(buf,len-1))
        return true;
   else
        return false;
}
void Tparameter::EnableSaveParam(bool flag)
{
   EnableSaveParamFlag = flag;
}
void Tparameter::WriteIntParam(AnsiString secName,AnsiString paramName,INT32 Param,UINT8 type)
{
     if(EnableSaveParamFlag)
     {
        if(fp == NULL)
          return;
        AnsiString str;
        TVarRec ip[4] = {(Param>>24)&0xff,(Param>>16)&0xff,(Param>>8)&0xff,Param&0xff};

       // TVarRec mac[6] = {0,(Param>>24)&0xff,(Param>>16)&0xff,(Param>>8)&0xff,Param&0xff,0xaa};
        switch(type)
        {
           case RD_U32_LOCAL_IP:

             str = str.Format("%d.%d.%d.%d",ip,4);
             fp->WriteString(secName,paramName,str);
             break;
           case RD_U32_REMOTE_IP:
             str = str.Format("%d.%d.%d.%d",ip,4);
             fp->WriteString(secName,paramName,str);
             break;
           case RD_U32_MAC_ADDR:
             str = str.Format("%x:%x:%x:%x",ip,4);
             str = "00:" + str + ":AA";
             fp->WriteString(secName,paramName,str);
             break;
           case RD_U32_SUB_ADDR:
             str = str.Format("%d.%d.%d.%d",ip,4);
             fp->WriteString(secName,paramName,str);
             break;
           case RD_U32_GAY_WAY_ADDR:
             str = str.Format("%d.%d.%d.%d",ip,4);
             fp->WriteString(secName,paramName,str);
             break;

           default:
             fp->WriteInteger(secName,paramName,Param);
             break;


        }
     }

}
void Tparameter::WriteFloatParam(AnsiString secName,AnsiString paramName,float Param)
{
     if(EnableSaveParamFlag)
     {
       AnsiString str = "";
       //fp->WriteFloat(secName,paramName,Param);
       str = FormatFloat("0.00000",Param);
       fp->WriteString(secName,paramName,str);
     }
}
int  Tparameter::PraseFrame(TCommunication* comm,UINT8* packetBuf, UINT32 len)
{
    return  CBusiness::PraseFrame(comm,packetBuf,  len);
  
}
void Tparameter::Disposal(TCommunication* commu,UINT8* packetBuf, UINT32 len)
{


        stTime myTime;
        UINT8 u8_temp;
	UINT16 u16_temp;
	UINT32 u32_temp;
	//float32 f32_temp;
	
	
	union
	{
	    float fl;
		struct{UINT8 fl4;UINT8 fl3;UINT8 fl2;UINT8 fl1;}sfl;
	}f32_temp;
        union
        {
            int i;
            struct{UINT8 u1;UINT8 u2;UINT8 u3;UINT8 u4;}u;
        }int32_temp;

        //if(!isValid(packetBuf,len))
        //    return;
        m_frame->packet.dir    =  packetBuf[OFFSET_CMD];
        m_frame->packet.cmd    =  packetBuf[OFFSET_CODE];
        m_frame->packet.dev_id =  (packetBuf[DEV_ADDR_HB]<<8)+(packetBuf[DEV_ADDR_LB]);

        m_frame->packet.param.__pad.pad[0] = packetBuf[OFFSET_INFO+3];
        m_frame->packet.param.__pad.pad[1] = packetBuf[OFFSET_INFO+2];
        m_frame->packet.param.__pad.pad[2] = packetBuf[OFFSET_INFO+1];
        m_frame->packet.param.__pad.pad[3] = packetBuf[OFFSET_INFO];
  

        

        NotifyView(commu,this);





}

